<?php


namespace Appstore\Controller;

use Appstore\Model;
use Think\Controller;

class UpdateController extends BaseController
{


    public function versions()
    {
        $versions=D('AppstoreWebsiteVersion')->select();
        echo json_encode($versions);
    }

    /**
     * 下载升级包
     */
    public function download(){
        $aType=I('get.type','new','text');

        $versionModel = D('AppstoreWebsiteVersion');
        $aNumber = I('get.number', 0, 'text');
        $version=$versionModel->where(array('number'=>$aNumber))->find();
        if (!$version) {
            $this->error('版本不存在。');
        } else {
            if($aType=='new'){
                if (intval($version['new_file']) == 0) {
                    $this->error('文件不存在。');
                }
                $versionModel->where(array('number' => $aNumber))->setInc('download_count');

                $fileModel = D('File');
                if ($fileModel->download(C('DOWNLOAD_UPLOAD.rootPath'), intval($version['new_file'])) == false) {
                    $this->error($fileModel->getError());
                };
            }else{
                if (intval($version['old_file']) == 0) {
                    $this->error('文件不存在。');
                }
                $versionModel->where(array('number' => $aNumber))->setInc('download_count');

                $fileModel = D('File');
                if ($fileModel->download(C('DOWNLOAD_UPLOAD.rootPath'), intval($version['old_file'])) == false) {
                    $this->error($fileModel->getError());
                };
            }

        }
    }

}